export function openDB(dbName, storeName, keyPath, version = 1) {
    return new Promise((resolve, reject) => {
        let request = indexedDB.open(dbName, version); //打开一个数据库，如果不存在则创建这个数据库。 返回IDBRequest实例
        let db


        request.onupgradeneeded = event => { //open后如不存在会调用此方法 创建数据库模式
            db = event.target.result
            if (db.objectStoreNames.contains(storeName)) {
                console.log("删除数据库模式")
                db.deleteObjectStore(storeName) //不删除会创建失败，因为名字相同
            }
            console.log("创建数据库模式")
            let store = db.createObjectStore(storeName, {
                keyPath: keyPath
            })
        }
        request.onerror = event => {
            console.log('打开失败:', event.target.errorCode)
            reject(event)
        }

        request.onsuccess = event => {
            console.log('打开成功:', event.target.result)
            db = event.target.result
            resolve(db)
        }
    })
}
export function addData(db, storeName, data) {
    return new Promise((resolve, reject) => {
        const transaction = db.transaction(storeName, "readwrite");
        const store = transaction.objectStore(storeName);
        let request
        request = store.add(data) //参数是对象
        request.onsuccess = (event) => {
            resolve(event)
        }
        request.onerror = (event) => {
            reject(event)
        }
    })

}
export function getData(db, storeName, keyPath) {
    return new Promise((resolve, reject) => {
        const transaction = db.transaction(storeName);
        const store = transaction.objectStore(storeName);
        let request
        request = store.get(keyPath) //参数是键
        request.onsuccess = (event) => {
            resolve(event)
        }
        request.onerror = (event) => {
            reject(event)
        }
    })

}
export function updateDb(db, storeName, data){
    return new Promise((resolve,reject)=>{
        const transaction = db.transaction(storeName, "readwrite");
    const store = transaction.objectStore(storeName);
    let request 
    
    request = store.put(data)//参数是对象
    request.onsuccess=(event)=>{
        // console.log('put成功:',event.target.result)//返回key
        resolve(event)
    }
    request.onerror=(event)=>{
        // console.log("put失败")
        reject(event)
    }
    })
    
}
export function deleteData(db, storeName, keyPath){
    return new Promise((resolve,reject)=>{
        const transaction = db.transaction(storeName, "readwrite");
    const store = transaction.objectStore(storeName);
    let request 
    request = store.delete(keyPath)//参数是键
    request.onsuccess=(event)=>{
        // console.log('delete成功')//永远成功
        resolve(event)
    }
    request.onerror=(event)=>{
        // console.log("delete失败")
        reject(event)
    }
    })
    
}
export function deleteDB(dbName){
    return new Promise((resolve, reject)=>{
        let request = indexedDB.deleteDatabase(dbName);
        request.onerror=event=>{
            reject(event)
        }
        request.onsuccess=event=>{
            resolve(event)
        }
    })
}
export default{
    openDB,
    addData,
    getData,
    updateDb,
    deleteData,
    deleteDB
}